<?php

namespace app\admin\controller\game;

use app\common\controller\Backend;

class Gameorder extends Backend
{
    protected $model = null;
    protected $userModel = null;
    protected $vipModel = null;


    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('common/Games/Gameorder');
        $this->userModel = model('common/Games/Gameuser');
        $this->vipModel = model('common/Games/Gamevip');
    }

    public function audit($ids = Null)
    {
        if ($this->request->isPost()) {
            //接收传过来数据
            $params = $this->request->param('row/a');

            // 为空处理和微信不存在的订单处理
            // 如果不为空，把data里面的金额装换位浮点型
            if (!empty($params['total'])) {
                $params['total'] = floatval($params['total']);
            } else {
                $data = [
                    'id' => $ids,
                    'auto' => 1,
                    'outcome' => '订单错误，请确认订单是否存在',
                ];

                // 保存修改
                $res = $this->model->isUpdate(true)->save($data);

                if ($res) {
                    $this->success('审核成功，订单金额为空');
                } else {
                    $this->error('审核失败' . $this->model->getError());
                }
            }

            //获取订单信息，包括左联的用户表和通过输入金额确定vip信息
            $info = $this->model->with(['gameuser'])->find($ids);
            $vipInfo = $this->vipModel->where(['money' => $params['total']])->find();


            //订单信息为空，就抛出错误
            if (empty($info)) {
                $this->error('订单数据不存在');
            }

            // 把用户信息和vip信息从接收来的数组中提取出来
            $userInfo = $info['gameuser'];

            //vip信息为空,说明此单金额不予vip订单里面任何套餐相似
            if (empty($vipInfo)) {
                $data = [
                    'id' => $ids,
                    'auto' => 1,
                    'outcome' => '订单金额不一致，请联系售后',
                ];

                $res = $this->model->isUpdate(true)->save($data);

                if ($res) {
                    $this->success('审核成功,金额不一致，套餐不存在');
                } else {
                    $this->error('审核失败' . $this->model->getError());
                }
            } else {
                // 开启事务
                $this->model->startTrans();
                $this->userModel->startTrans();

                // 再去获取vip信息里面的金额和vip持续时间
                $vipTime = $vipInfo['times'];

                $data = [
                    'id' => $ids,
                    'auto' => 1,
                    'outcome' => '审核成功',
                    'vipid' => $vipInfo['id'],
                    'total' => $params['total'],
                ];

                // 插入数据
                $res = $this->model->isUpdate(true)->save($data);

                // 如果插入失败就回滚然后保存
                if (!$res) {
                    $this->model->rollback();
                    $this->error('审核失败' . $this->model->getError());
                }

                // 插入用户数据
                // 先判断用户的会员结束时间是否存在，且是否过期
                if ($userInfo['vipendtime'] && $userInfo['vipendtime'] > time()) {
                    $userdata = [
                        'id' => $userInfo['id'],
                        'vipendtime' => $userInfo['vipendtime'] + $vipTime,
                    ];
                } else {
                    $userdata = [
                        'id' => $userInfo['id'],
                        'vipendtime' => time() + $vipTime,
                    ];
                }
                // 插入用户数据
                $userRes = $this->userModel->isUpdate(true)->save($userdata);

                if (!$userRes) {
                    $this->userModel->rollback();
                    $this->error('审核失败' . $this->userModel->getError());
                }

                // 提交事务
                $this->model->commit();
                $this->userModel->commit();

                $this->success('审核成功,会员时长增加'.$vipInfo['name']);

            }
        }

        return $this->view->fetch();
    }
}
